var Listbox = {
    active : 1,
	count : 5,
	lid : 0,
	fadetime : 400,
	path : "",
	lock : false,
	
	timer : false,
	
	// data
	
	dataContent : new Array(),
	dataImage : new Array(),
	
	// end data
	
	loadContent : function(){
		$.ajax({
			type: "POST",
			url: Cyfy.getSourcePath('ajax.php'),
			data: "module=ListBoxes&ajax=loadData&lid=" + this.lid + "&active=" + this.active,
			async: true,
			dataType: "json",
			success: function(data){
				Listbox.dataContent[Listbox.active] = data.content;
				Listbox.dataImage[Listbox.active] = data.image;
				Listbox.setContent();
				Listbox.lock = false;
			}
		});
	},
	
	
	show : function(i){
		
		if(this.lock == true) return;
	
		this.active = i;
		
		this.lock = true;
		
		
		// is in cache?
		if(this.dataContent[this.active] != undefined){
			// set
			this.setContent();
			// set active
			this.storeActive();
		}else{
			// ajax
			this.loadContent();
		}
		
		
},
	
	storeActive : function(){
		Cyfy.ajax("ListBoxes", "storeActive", "", "lid=" + this.lid + "&active=" + this.active);
	},
    
	setContent : function(){
		
		
		// change the color
		$(".listbox-pager li").removeAttr("id");
		$(".listbox-pager li").eq(this.active).attr("id", "listbox-active");
		
		// fadeout old picture
		
		$(".listbox-image").fadeOut(this.fadetime, function(){
			$(this).remove();
			Listbox.lock = false;
		});
		
		// fadein new picture
		$("<img class=\"listbox-image\" src=\"" + Cyfy.getSourcePath(this.path + this.dataImage[this.active]) + "\" alt=\"\" />").insertBefore(".listbox-image");
		
		// fade out old text
		$(".listbox-content").fadeOut(this.fadetime, function(){
			$(this).remove();
		});
		
		// fade in new text
		$("<div class=\"listbox-content\">" + this.dataContent[this.active] + "</div>").insertBefore(".listbox-content");
        
        // cufon
        this.cufon();
	},
	
    showNext : function(){

		if(this.count == 1) return false;

		if(this.active + 1 <= this.count){
			this.show(this.active + 1);
		}else{
			this.show(1);
		}
    },
	
	showPrev: function(){
		
		if(this.count == 1) return false;
	
		if(this.active - 1 >= 1){
			this.show(this.active - 1);
		}else{
			this.show(this.count);
		}
	},
    
    cufon: function()
    {
        if(window.Cufon !== undefined)
        {
            // cufon
            Cufon.replace('.listbox-content p')('.listbox-content h2')
        }
    }
	
	

}


$(document).ready(function(){
	$(".listbox-pager a").click(function(){
		
		// resets interval
		if(Listbox.timer){
			clearInterval(Listbox.timer);
			Listbox.timer = setInterval("Listbox.showNext()",6000);
		}
		
		var value = $(this).attr("href").substr(1);
		
		if(value == "next"){
			Listbox.showNext();
		}else if(value == "prev"){
			Listbox.showPrev();
		}else{
			Listbox.show(parseInt(value));
		}
		
		return false;
	});
    
    Listbox.cufon();
	
	Listbox.timer = setInterval("Listbox.showNext()",6000); 
});